package com.nextar.ui.admin.doctor;

import static com.nextar.ui.NextarConstants.COMMON_FIELD_WIDTH;

import java.util.Date;

import com.nextar.ui.NextarConstantsImpl;
import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

public class DoctorFieldFactory extends DefaultFieldFactory{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6521581937077333929L;

	final ComboBox productsTobe = new ComboBox("Products to be focused");
	final ComboBox productsAtPresent = new ComboBox("Products Currently focused");
	final PopupDateField visitDate = new PopupDateField("Date of Visit");
	final TextArea address = new TextArea("Address");
	
	public DoctorFieldFactory() {
		productsTobe.setWidth(COMMON_FIELD_WIDTH);
		for(String item: NextarConstantsImpl.getNextarProducts()){
			productsTobe.addItem(item);
			productsAtPresent.addItem(item);
		}		
		
		productsAtPresent.setWidth(COMMON_FIELD_WIDTH);
		//productsAtPresent.setMultiSelect(true);
		
		productsTobe.setWidth(COMMON_FIELD_WIDTH);
		//productsTobe.setMultiSelect(true);
		
		visitDate.setData(new Date());
		visitDate.setResolution(PopupDateField.RESOLUTION_DAY);
		
		address.setRequired(true);
	}
	
	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if("productsTobeFocused".equals(propertyId)){
			f = productsTobe;
		}else if("productsAtPresent".equals(propertyId)){
			f = productsAtPresent;
		}else if("dateOfVisit".equals(propertyId)){
			f = visitDate;
		}else if("address".equals(propertyId)){
			f = address;
		}else{
			f = super.createField(item, propertyId, uiContext);
		}	
		
		if ("name".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setRequiredError("Please enter a Doctor Name");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Doctor's Name must be 3-25 characters", 3, 25, false));
        } else if("speciality".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setRequiredError("Please enter a Speciality");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Speciality must be 3-25 characters", 3, 25, false));
        }else if("area".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setRequiredError("Please enter a area");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Area must be 3-25 characters", 3, 25, false));
        }else if("rating".equals(propertyId)) {
            TextField tf = (TextField) f;
            tf.setRequired(true);
            tf.setRequiredError("Please select Date of Visit");
            tf.setWidth(COMMON_FIELD_WIDTH);
            tf.addValidator(new StringLengthValidator(
                    "Date of visit must be selected"));
        }else{
        	
        }
        	
        
		return f;
	}
}
